# Contributor: Charlie Barto <https://github.com/barcharcraz>

_realname=ledger
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.3.2
pkgrel=5
pkgdesc="Double-entry accounting system with a command-line reporting interface. (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://ledger-cli.org/"
msys2_repository_url="https://github.com/ledger/ledger"
msys2_references=(
  'archlinux: ledger'
  "cpe: cpe:2.3:a:ledger-cli:ledger"
)
license=("spdx:BSD-3-Clause")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-boost-libs"
         "${MINGW_PACKAGE_PREFIX}-mpfr"
         "${MINGW_PACKAGE_PREFIX}-gmp")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-utf8cpp"
             "${MINGW_PACKAGE_PREFIX}-python")
source=("https://github.com/ledger/ledger/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-fix-build-with-boost-1.85.patch::https://github.com/ledger/ledger/commit/1da89f6f.patch"
        "002-fix-build-with-boost-1.86.patch")
sha256sums=('555296ee1e870ff04e2356676977dcf55ebab5ad79126667bc56464cb1142035'
            '4f39401338535a57078e7dc183d5a2eb7ccf9907d43219065cab7d00085c75a0'
            '0e3fbde90f1a5e9d27a53aa59d9b9b517042e5a4b7c19f0b9033d10534f75b8b')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/001-fix-build-with-boost-1.85.patch
  # https://github.com/ledger/ledger/pull/2382
  patch -p1 -i "${srcdir}"/002-fix-build-with-boost-1.86.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} &&  cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release" "-DBUILD_DEBUG=OFF")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug"  "-DBUILD_DEBUG=ON")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -GNinja\
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
    "${_extra_config[@]}" \
    -DBUILD_DOCS=OFF \
    -DBUILD_LIBRARY=OFF \
    -DBUILD_WEB_DOCS=OFF \
    -DCMAKE_GNUtoMS=OFF \
    -DUSE_DOXYGEN=OFF \
    -DUSE_GPGME=OFF \
    -DUSE_PYTHON=OFF \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE.md \
    -t "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}
}
